<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/4/13
 * Time: 16:41
 */

namespace App\HttpController\V1;

use App\Tools\Token\Token;
use EasySwoole\EasySwoole\Config;
use App\HttpController\Taoran\Functions\Publics;
use App\Common\Qiniuyun;

class DbWithCommon extends BaseWithDb
{

    /**
     * 七牛云多文件上传
     * @return bool
     */
    function uploadFile()
    {
        $request = $this->request();
        $Qiniu = new Qiniuyun();
        $img_file = $request->getUploadedFile('file');
        $arr = null;
        if ($img_file) {
            foreach ($img_file as $k => $v) {
                $arr[] = QINIU_URL . $Qiniu->update($v->getTempName());
            }
        }
        return $this->writeJson(200, $arr, '成功');
    }

    /**
     * 视频列表
     */
    function video_list()
    {
        $id = $this->input('id');
        $type = $this->input('type') ?: 1;  // 0上拉  1下拉
        $where = $this->input('where') ?: 0; //0首页  1关注  2个人详情  3收藏
        $his_user_id=$this->input('user_id');//对方user_id
        $page = $this->getPage();
        $condition = $type === 1 ? '<=' : '>=';
        $field = 'id,study_title,user_id,vip_video,`like`,favorite,comm_num,create_time,video_img,widths,heights,browse';
        $jwt = $this->request()->getHeader(Config::getInstance()->getConf('TOKEN_NAME'));
        if (!empty($jwt)) {
            $token = Token::decode($jwt[0]);
            $content = (array)$token['content'];
        } else {
            $content = null;
        }
        $user_id = null;
        if (isset($content['id'])) {
            $user_id = $content['id'];
        }
        $info = [];
        switch ($where) {
            case 0:
                $info = $this->db->where('class_id', 47)->whereNotNull('video_img')->where("id$condition$id")->orderBy('id', 'desc')->get('study', $page, $field);
                break;
            case 1:
                $my_follow = $this->db->where('user_id', $user_id)->get('users_follow');
                $arr_userid = array_column($my_follow, 'to_user_id');
                if (!empty($arr_userid)) {
                    $this->db->whereIn('user_id', $arr_userid);
                    $info = $this->db->where('class_id', 47)->whereNotNull('video_img')->where("id$condition$id")->orderBy('id', 'desc')->get('study', $page, $field);
                }
                break;
            case 2:
                $info = $this->db->where('class_id', 47)->where('user_id', $his_user_id)->whereNotNull('video_img')->where("id$condition$id")->orderBy('id', 'desc')->get('study', $page, $field);
                break;
            case 3:
                $my_collect = $this->db->where('class_id', 47)->where('user_id', $user_id)->get('users_collection');
                $arr_studyid = array_column($my_collect, 'study_id');
                if (!empty($arr_studyid)) {
                    $this->db->whereIn('id', $arr_studyid);
                    $info = $this->db->where('class_id', 47)->whereNotNull('video_img')->where("id$condition$id")->orderBy('id', 'desc')->get('study', $page, $field);
                }
                break;
            default:
                return $this->writeJson(-3, null, '参数错误');
        }
        if($his_user_id){
            $user_id=$his_user_id;
        }
        if (!empty($info)) {
            foreach ($info as $k => &$v) {
                $v['is_my']=$user_id==$v['user_id']?1:0;
                $this->db->where('id', $v['id'])->setInc('study', 'browse', 1);
                $user = $this->db->where('id', $v['user_id'])->getOne('users', 'headimg,nickname');
                $v['video_img'] = QINIU_URL . $v['video_img'];
                $v['vip_video'] = QINIU_URL . $v['vip_video'];
                $v['user_headimg'] = QINIU_URL . $user['headimg'];
                $v['user_nickname'] = $user['nickname'];
                $v['is_follow'] = 0;
                if ($user_id != null) {
                    //判断 关注
                    $follow = $this->db->where('user_id', $user_id)->where('to_user_id', $v['user_id'])->has('users_follow');
                    $follow = $follow ? 1 : 0;
                    $v['is_follow'] = $follow;
                }
            }
        }
        return $this->writeJson(200, $info, '成功');
    }

    /**
     * 查看评论
     */
    function select_comment()
    {
        $page = $this->getPage();
        $Publics = new Publics();
        $id = $this->input('id');      //  study_id
        $comment_id = $this->input('comment_id');
        $info = $this->db->where('study_id', $id)->orderBy('id')->get('study_comment', $page, '*');
        $count = $this->db->where('id', $id)->getValue('study', 'comm_num');
        foreach ($info as $k => &$v) {
            $v['create_time'] = $Publics->get_last_time($v['create_time']);
            $user = $this->db->where('id', $v['user_id'])->getOne('users', 'headimg,nickname');
            $v['user_headimg'] = QINIU_URL . $user['headimg'];
            $v['user_nickname'] = $user['nickname'];
            if ($v['to_user_id']) {
                $to_user = $this->db->where('id', $v['to_user_id'])->getOne('users', 'headimg,nickname');
                $to_user_content = $this->db->where('id', $v['pid'])->getValue('study_comment', 'content');
                $v['to_user_info']['user_headimg'] = QINIU_URL . $to_user['headimg'];
                $v['to_user_info']['user_nickname'] = $to_user['nickname'];
                $v['to_user_info']['user_content'] = $to_user_content;
            }
        }
        if ($comment_id) {
            $comment = $this->db->where('id', $comment_id)->getOne('study_comment', '*');
            $comment['create_time'] = $Publics->get_last_time($comment['create_time']);
            $user = $this->db->where('id', $comment['user_id'])->getOne('users', 'headimg,nickname');
            $comment['user_headimg'] = QINIU_URL . $user['headimg'];
            $comment['user_nickname'] = $user['nickname'];
            if ($comment['to_user_id']) {
                $to_user = $this->db->where('id', $comment['to_user_id'])->getOne('users', 'headimg,nickname');
                $to_user_content = $this->db->where('id', $comment['pid'])->getValue('study_comment', 'content');
                $comment['to_user_info']['user_headimg'] = QINIU_URL . $to_user['headimg'];
                $comment['to_user_info']['user_nickname'] = $to_user['nickname'];
                $comment['to_user_info']['user_content'] = $to_user_content;
            }
            $info[0] = $comment;
        }
        return $this->writeJson(200, ['count' => $count, 'list' => $info], '成功');
    }

    /**
     * 炫炫首页
     * @return bool
     */
    function select_all()
    {
        $page = $this->getPage();
        $Publics = new Publics();
        $class_id = $this->db->where('pid', 2)->getValue('study_class', 'id');
        $jwt = $this->request()->getheader(config::getinstance()->getconf('TOKEN_NAME'));
        if (!empty($jwt)) {
            $token = Token::decode($jwt[0]);
            $content = (array)$token['content'];
        } else {
            $content = null;
        }
        $user_id = null;
        if (isset($content['id'])) {
            $user_id = $content['id'];
        }
        $data = $this->db
            ->where('class_id', $class_id)
            ->orderBy('id')
            ->get('study', $page, "id,study_title,user_id,image,vip_video,`like`,favorite,comm_num,create_time,video_img,widths,heights,browse");
        foreach ($data as $k => &$v) {
            $v['is_my']=$v['user_id']==$user_id?1:0;
            if ($v['image']) {
                $images = explode(';', $v['image']);
                foreach ($images as &$val) {
                    $val = QINIU_URL . $val;
                }
                $v['image'] = $images;
            } else {
                $v['image'] = [];
            }
            if ($v['vip_video']) {
                $v['vip_video'] = QINIU_URL . $v['vip_video'];
                $v['video_img'] = QINIU_URL . $v['video_img'];
            }
            $v['create_time'] = $Publics->get_last_time($v['create_time']);
            $v['is_like'] = 0;
            $v['is_collect'] = 0;
            $v['is_follow'] = 0;
            if ($user_id != null) {
                //判断收藏 关注 点赞
                $like = $this->db->where('user_id', $user_id)->where('study_id', $v['id'])->has('study_like');
                $collect = $this->db->where('user_id', $user_id)->where('study_id', $v['id'])->has('users_collection');
                $follow = $this->db->where('user_id', $user_id)->where('to_user_id', $v['user_id'])->has('users_follow');
                $follow = $follow ? 1 : 0;
                $like = $like ? 1 : 0;
                $collect = $collect ? 1 : 0;
                $v['is_like'] = $like;
                $v['is_collect'] = $collect;
                $v['is_follow'] = $follow;
            }
            $users = $this->db->where('id', $v['user_id'])->getOne('users', 'nickname,headimg');
            $v['nickname'] = $users['nickname'];
            $v['headimg'] = QINIU_URL . $users['headimg'];
            $v['comment'] = $this->db->where('study_id', $v['id'])->whereNull('to_user_id')->orderBy('id')->get('study_comment', 3, 'id,user_id,content');
            if ($v['comment']) {
                foreach ($v['comment'] as $ks => &$vs) {
                    $vs['user_nickname'] = $this->db->where('id', $vs['user_id'])->getValue('users', 'nickname');
                }
            }
        }
        return $this->writeJson(200, $data, '成功');
    }

    /**
     * 系统通知
     * @return bool
     */
    function guide_list()
    {
        $page = $this->getPage();
        $data = $this->db
            ->orderBy('id')
            ->get('guide', $page, 'id,title,create_time');
        $jwt = $this->request()->getHeader(Config::getInstance()->getConf('TOKEN_NAME'));
        if (!empty($jwt)) {
            $token = Token::decode($jwt[0]);
            $content = (array)$token['content'];
        } else {
            $content = null;
        }
        $user_id = null;
        if (isset($content['id'])) {
            $user_id = $content['id'];
        }
        foreach ($data as $k => &$v) {
            if ($user_id != null) {
                $read = $this->db->where('guide_id', $v['id'])->where('user_id', $user_id)->has('guide_read');
                $v['read'] = $read ? 1 : 0;
            } else {
                $v['read'] = 1;
            }
        }
        return $this->writeJson(200, $data, '成功');
    }

    /**
     * 系统通知详情
     * @return bool
     */
    function guide_detail()
    {
        $id = $this->input('id');
        $data = $this->db->where('id', $id)->getOne('guide', 'id,content,create_time');
        return $this->writeJson(200, $data, '成功');
    }

    /**
     * 更改系统通知状态
     */
    function guide_read()
    {
        $id = $this->input('id');
        $jwt = $this->request()->getHeader(Config::getInstance()->getConf('TOKEN_NAME'));
        if (!empty($jwt)) {
            $token = Token::decode($jwt[0]);
            $content = (array)$token['content'];
            $user_id = null;
            if (isset($content['id'])) {
                $user_id = $content['id'];
            }
            if ($user_id != null) {
                if (!$this->db->where('id', $id)->where('user_id', $user_id)->has('guide_read')) {
                    $this->db->insert('guide_read', ['user_id' => $user_id, 'guide_id' => $id]);
                }
            }

        }
        return $this->writeJson(200, null, '成功');
    }

    /**
     * 回复列表
     */
    function select_replay()
    {
        $page = $this->getPage();
        $data = [];
        $jwt = $this->request()->getHeader(Config::getInstance()->getConf('TOKEN_NAME'));

        if (!empty($jwt)) {
            $token = Token::decode($jwt[0]);
            $content = (array)$token['content'];
            $user_id = null;
            if (isset($content['id'])) {
                $user_id = $content['id'];
            }
            if ($user_id != null) {
                $data = $this->db
                    ->where('to_user_id', $user_id)
                    ->orderBy('id', 'desc')
                    ->get('study_comment', $page, '*');
                foreach ($data as $k => &$v) {
                    $user = $this->db->where('id', $v['user_id'])->getOne('users', 'headimg,nickname');
                    $v['headimg'] = QINIU_URL . $user['headimg'];
                    $v['nickname'] = $user['nickname'];
                    $study_info = $this->db->where('id', $v['study_id'])->getOne('study', 'study_title,class_id,name');
                    if ($study_info) {
                        $calss_id = $this->db->where('pid', 9)->get('study_class', null, 'id');
                        $ids = array_column($calss_id, 'id');
                        if (in_array($study_info['class_id'], $ids)) {
                            $v['study_title'] = '【' . $study_info['study_title'] . '】' . ' ' . $study_info['name'];
                        } else {
                            $v['study_title'] = $study_info['study_title'];
                        }
                    } else {
                        $v['study_title'] = '该信息已被删除';
                    }
                }
            }
        }
        return $this->writeJson(200, $data, '成功');
    }

    /**
     * 获取读取状态
     * @return int
     */
    function msg()
    {
        $jwt = $this->request()->getHeader(Config::getInstance()->getConf('TOKEN_NAME'));
        $read = 1;

        if (!empty($jwt)) {
            $token = Token::decode($jwt[0]);
            $content = (array)$token['content'];
            $user_id = null;
            if (isset($content['id'])) {
                $user_id = $content['id'];
            }
            if ($user_id != null) {
                $data = $this->db
                    ->get('guide');
                foreach ($data as $k => &$v) {
                    $is_read = $this->db->where('guide_id', $v['id'])->where('user_id', $user_id)->has('guide_read');
                    if (!$is_read) {
                        $read = 0;
                        break;
                    }
                }
                $comment = $this->db
                    ->where('to_user_id', $user_id)
                    ->where('status', 0)
                    ->has('study_comment');
                if ($comment) {
                    $read = 0;
                }
            }
        }
        return $this->writeJson(200, $read, '成功');
    }

    /**
     * 首页活动弹窗
     * @return bool
     */
    function index_activity()
    {
        $data = $this->db->where('class_id', 46)->orderBy('id')->getOne('study', 'id,image');
        if ($data) {
            $data['image'] = QINIU_URL . $data['image'];
        } else {
            $data = [];
        }
        return $this->writeJson(200, $data, '成功');
    }

    /**
     * 个人中心动态
     */
    function my_video_list()
    {
        $page = $this->getPage();
        $class_id = $this->db->where('pid', 2)->getValue('study_class', 'id');
        $his_user_id = $this->input('user_id');
        $user_info = $this->db->where('id', $his_user_id)->getOne('users');
        $jwt = $this->request()->getHeader(Config::getInstance()->getConf('TOKEN_NAME'));
        if (!empty($jwt)) {
            $token = Token::decode($jwt[0]);
            $content = (array)$token['content'];
        } else {
            $content = null;
        }
        $user_id = null;
        if (isset($content['id'])) {
            $user_id = $content['id'];
        }
        $is_follow = $this->db->where('user_id', $user_id)->where('to_user_id', $his_user_id)->has('users_follow');
        $is_follow = $is_follow ? 1 : 0;
        $data = $this->db
            ->where('class_id', $class_id)
            ->where('user_id', $his_user_id)
            ->orderBy('id', 'desc')
            ->get('study', $page, "id,study_title,user_id,image,vip_video,`like`,favorite,comm_num,create_time,video_img,widths,heights,browse");
        $count = $this->db
            ->where('class_id', $class_id)
            ->where('user_id', $his_user_id)
            ->count('study');
        $Publics = new Publics();
        foreach ($data as $k => &$v) {
            if ($v['image']) {
                $images = explode(';', $v['image']);
                foreach ($images as &$val) {
                    $val = QINIU_URL . $val;
                }
                $v['image'] = $images;
            } else {
                $v['image'] = [];
            }
            if ($v['vip_video']) {
                $v['vip_video'] = QINIU_URL . $v['vip_video'];
                $v['video_img'] = QINIU_URL . $v['video_img'];
            }
            $v['create_time'] = $Publics->get_last_time($v['create_time']);
            $v['is_like'] = 0;
            $v['is_collect'] = 0;
            $v['is_follow'] = 0;
            if ($user_id != null) {
                //判断收藏 关注 点赞
                $like = $this->db->where('user_id', $user_id)->where('study_id', $v['id'])->has('study_like');
                $collect = $this->db->where('user_id', $user_id)->where('study_id', $v['id'])->has('users_collection');
                $follow = $this->db->where('user_id', $user_id)->where('to_user_id', $v['user_id'])->has('users_follow');
                $follow = $follow ? 1 : 0;
                $like = $like ? 1 : 0;
                $collect = $collect ? 1 : 0;
                $v['is_like'] = $like;
                $v['is_collect'] = $collect;
                $v['is_follow'] = $follow;
            }
            $users = $this->db->where('id', $v['user_id'])->getOne('users', 'nickname,headimg');
            $v['nickname'] = $users['nickname'];
            $v['headimg'] = QINIU_URL . $users['headimg'];
            $v['comment'] = $this->db->where('study_id', $v['id'])->whereNull('to_user_id')->orderBy('id')->get('study_comment', 3, 'id,user_id,content');
            if ($v['comment']) {
                foreach ($v['comment'] as $ks => &$vs) {
                    $vs['user_nickname'] = $this->db->where('id', $vs['user_id'])->getValue('users', 'nickname');
                }
            }
        }
        return $this->writeJson(200, [
            'content' =>
                [
                    'count' => $count,
                    'headimg' => QINIU_URL . $user_info['headimg'],
                    'nickname' => $user_info['nickname'],
                    'is_follow' => $is_follow,
                    'is_my'=>$his_user_id==$user_id?1:0,
                ],
            'data' => $data], '成功');
    }


}